<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/17
 * Time: 13:47
 */
namespace app\mtsuperadmin\controller;

use app\mtadminapi\model\MtAdminSysAccountRole;
use app\mtadminapi\model\MtAdminSysAuth;
use app\mtadminapi\model\MtAdminSysRole;
use app\mtadminapi\model\MtAdminSysRoleAuth;
use app\util\ReturnCode;

/**
 * 权限管理
 * Class AdminAuthManage
 * @package app\mtsuperadmin\controller
 */
class AdminAuthManage extends Base
{
    /**
     * 角色的修改添加
     * @return array|void
     */
    public function roleUpdate(){
        $roleName = $this->request->post("roleName");
        $status = intval($this->request->post("status"));
        if($roleName){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $obj = new MtAdminSysRole();
        $roleNameCount = $obj->where(["roleName","=",$roleName],["status","=","1"])->count();
        if ($roleNameCount){
            $res = $obj->update(["mTime"=>time(),"status"=>$status],["roleName"=>$roleName]);
        }else{
            $res = $obj->insert(["cTime"=>time(),"roleName"=>$roleName,"status"=>$status],true);
        }
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }

    /**
     * 角色权限修改添加
     * @return array|void
     */
    public function roleAuthUpdate(){
        $roleID = intval($this->request->post("roleID"));
        $roleAuth = $this->request->post("roleAuth");
        $status = intval($this->request->post("status"));
        if($roleID || $roleAuth){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $obj = new MtAdminSysRoleAuth();
        $roleNameCount = $obj->where(["roleID","=",$roleID],["status","=","1"])->count();
        if ($roleNameCount){
            $res = $obj->update(["mTime"=>time(),"roleAuth"=>$roleAuth,"status"=>$status],["roleID"=>$roleID]);
        }else{
            $res = $obj->insert(["cTime"=>time(),"roleID"=>$roleID,"roleAuth"=>$roleAuth],true);
        }
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }

    /**
     * 权限的修改添加
     * @return array
     */
    public function authUpdate(){
        $data = $this->request->post("data");
        if(!$data){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $obj = new MtAdminSysAuth();
        if(isset($data['id'])){
            $data['mTime'] = time();
            $res = $obj->update($data,["id"=>$data['id']]);
        }else{
            $data['cTime'] = time();
            $res = $obj->insert($data,true);
        }
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }

    /**
     * 绑定管理员到角色
     * @return array|void
     */
    public function bindAdminRole(){
        $adminID = intval($this->request->post("adminID"));
        $roleID = intval($this->request->post("roleID"));
        $status = intval($this->request->post("status"));
        if($adminID || $roleID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $obj = new MtAdminSysAccountRole();
        $roleNameCount = $obj->where(["adminID","=",$adminID],["status","=","1"])->count();
        if ($roleNameCount){
            $res = $obj->update(["mTime"=>time(),"roleID"=>$roleID,"status"=>$status],["adminID"=>$adminID]);
        }else{
            $res = $obj->insert(["cTime"=>time(),"adminID"=>$adminID,"roleID"=>$roleID],true);
        }
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }
}